package team.seekers.catering.food.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.seekers.catering.common.result.R;
import team.seekers.catering.common.result.ResultCode;
import team.seekers.catering.food.entity.dao.Combo;
import team.seekers.catering.food.entity.dto.add.ComboAddDTO;
import team.seekers.catering.food.entity.dto.update.ComboUpdateDTO;
import team.seekers.catering.food.entity.dto.query.ComboQuery;
import team.seekers.catering.food.entity.vo.info.ComboInfoVO;
import team.seekers.catering.food.service.ComboService;
import team.seekers.catering.food.service.FoodService;
import team.seekers.catering.food.service.IngredientService;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/combo")
public class ComboController {

    @Autowired
    private ComboService comboService;
    @Autowired
    private FoodService foodService;


    /**
     * 新增套餐
     */
    @PostMapping("/add")
    public R add(@RequestBody ComboAddDTO comboAddDTO){
        // 根据单品id集合得出食材组成并计算营养成分
        foodService.setParamFromList(comboAddDTO);
        String id = comboService.add(comboAddDTO);
        return R.success().data("id",id);
    }

    /**
     * 删除套餐
     */
    @GetMapping("/delete/{packageId}")
    public R delete(@PathVariable("packageId")String id){
        comboService.removeById(id);
        return R.success();
    }

    /**
     * 修改套餐
     */
    @PostMapping("/update")
    public R update(@RequestBody ComboUpdateDTO comboUpdateDTO){
        comboUpdateDTO = comboService.setParamFromList(comboUpdateDTO);
        String id = comboService.update(comboUpdateDTO);
        return R.success().data("id",id);
    }

    /**
     * 查询套餐
     */
    @PostMapping("/adminPageOrder/{current}/{limit}")
    public R adminPageOrder(@PathVariable("current") long current,
                            @PathVariable("limit") long limit,
                            @RequestBody ComboQuery query){
        QueryWrapper<Combo> wrapper = new QueryWrapper<>();
        if(!StringUtils.isNullOrEmpty(query.getNameKey())){
            wrapper.like("name",query.getNameKey());
        }
        Map<String, Object> map = comboService.adminPageOrder(current,limit,wrapper);
        if(map == null){
            return R.fail(ResultCode.GET_FAIL);
        }
        return R.success(map);
    }

    /**
     * 套餐详情
     */
    @GetMapping("/query/{comboId}")
    public R query(@PathVariable("comboId")String id){
        ComboInfoVO comboInfoVO = comboService.get(id);
        return R.success().data("info", comboInfoVO);
    }


}
